package com.onetech.adrewards.free.dialogs;

import android.app.Activity;
import android.view.View;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.activities.SplashScreenActivity.ExitDialogHandler;
import com.onetech.adrewards.free.utils.PackageHelper;
import com.zuzu.dialogs.BaseDialog;

public class UpdateDialog extends BaseDialog {
	private boolean isForceUpdate=false;
	private Activity activity=null;
	private String packageName="";
	
	private Tracker mTracker;
	private ExitDialogHandler mHandler=null;
	
	public UpdateDialog(Activity context,boolean _isForceUpdate,String _packageName,ExitDialogHandler _handler){
		super(context,context.getString(R.string.dialog_update_title),TYPE_2_BUTTON,R.layout.dialog_update_layout);
		activity=context;
		isForceUpdate=_isForceUpdate;	
		packageName=_packageName;
		mHandler=_handler;
		if(isForceUpdate){
			setNegativeButtonTitle(R.string.dialog_force_update_button_title_Cancel);
		}else{
			setNegativeButtonTitle(R.string.dialog_normal_update_button_title_Cancel);
		}
		if(isForceUpdate){
			setPositiveButtonTitle(R.string.dialog_force_update_button_title_Ok);
		}else{
			setPositiveButtonTitle(R.string.dialog_normal_update_button_title_Ok);
		}
		setDefaultButtonTitle(R.string.dialog_force_update_button_title_Ok);
		
		setOnDialogListener(new OnDialogListener() {			
			@Override
			public void onPositiveButtonClicked() {
				try {
					PackageHelper.getInstance(getContext()).activeApp(packageName);
				} catch (Exception e) {
				}
			}
			
			@Override
			public void onNegativeButtonClicked() {
				if(isForceUpdate){
					//activity.moveTaskToBack(true);
					//activity.finish();
					mHandler.doExit(isForceUpdate);
				}else{
					dismiss();
					mHandler.go2HomeActivity();					
				}
			}
			
			@Override
			public void onDefaultButtonClicked() {
				dismiss();
			}
		});
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.exitDialogBtn:
			if(isForceUpdate){
				mHandler.doExit(isForceUpdate);
			}else{
				dismiss();
				mHandler.go2HomeActivity();				
			}
			break;

		default:
			super.onClick(v);
			break;
		}
	}
	@Override
    public void onStart() {
        super.onStart();
        try {
            mTracker.set(Fields.SCREEN_NAME, activity.getString(R.string.UpdateDialog));            
            mTracker.send(MapBuilder.createAppView().build());            
		} catch (Exception e) {
		}
    }
}
